#include "qttools.h"
#include <QtLua/Function>
#include <QMessageBox>
#include <QComboBox>
#include <QInputDialog>
#include <QFileDialog>
#include <QTextStream>
#include <QDate>

Q_EXPORT_PLUGIN2(qt-tools, QtTools)

const QtLua::String & QtTools::get_name() const
{
    static const QtLua::String s("qt-tools");
    return s;
}

const QtLua::String & QtTools::get_description() const
{
    static const QtLua::String s("Some Qt tools");
    return s;
}

void QtTools::register_members(QtLua::Plugin &plugin)
{

    static class : public QtLua::Function
    {
        QtLua::Value::List meta_call(QtLua::State &ls, const QtLua::Value::List &args)
        {
            QtLua::String title = get_arg<QtLua::String>(args, 0);
            QtLua::String text = get_arg<QtLua::String>(args, 1);
            QMessageBox::information(0, QString::fromUtf8(title.constData()), QString::fromUtf8(text.constData()));
            return QtLua::Value(ls, 0);
        }

    } info_box;

    static class : public QtLua::Function
    {
        QtLua::Value::List meta_call(QtLua::State &ls, const QtLua::Value::List &args)
        {
            QtLua::String title = get_arg<QtLua::String>(args, 0);
            QtLua::String text = get_arg<QtLua::String>(args, 1);
            QMessageBox::warning(0, QString::fromUtf8(title.constData()), QString::fromUtf8(text.constData()));
            return QtLua::Value(ls, 0);
        }

    } warning_box;

    static class : public QtLua::Function
    {
        QtLua::Value::List meta_call(QtLua::State &ls, const QtLua::Value::List &args)
        {
            QtLua::String title = get_arg<QtLua::String>(args, 0);
            QtLua::String text = get_arg<QtLua::String>(args, 1);
            int val = get_arg<int>(args, 2, 0);
            int min = get_arg<int>(args, 3, 0);
            int max = get_arg<int>(args, 4, 100);
            int step = get_arg<int>(args, 5, 1);
            bool ok;
            int getted = QInputDialog::getInt(0, QString::fromUtf8(title.constData()),
                                              QString::fromUtf8(text.constData()),
                                              val, min, max, step, &ok);
            if (!ok)
                return QtLua::Value(ls, -1);
            return QtLua::Value(ls, getted);
        }

    } get_int;

    static class : public QtLua::Function
    {
        QtLua::Value::List meta_call(QtLua::State &ls, const QtLua::Value::List &args)
        {
            QtLua::String title = get_arg<QtLua::String>(args, 0);
            QtLua::String filter = get_arg<QtLua::String>(args, 1);

            QString file = QFileDialog::getSaveFileName(0, QString::fromUtf8(title.constData()),
                                         QString(), filter);

            return QtLua::Value(ls, file);
        }

    } get_filename;

    static class : public QtLua::Function
    {
        QtLua::Value::List meta_call(QtLua::State &ls, const QtLua::Value::List &args)
        {
            QtLua::String filename = get_arg<QtLua::String>(args, 0);
            QtLua::String text = get_arg<QtLua::String>(args, 1);

            QFile file(filename);
            if (file.open(QIODevice::WriteOnly)) {
                QTextStream out(&file);
                out.setCodec("Windows-1251");
                out << QString::fromUtf8(text.constData());
                file.close();
                return QtLua::Value(ls, 1);
            } else {
                return QtLua::Value(ls, 0);
            }
        }

    } write_text_file;

    static class : public QtLua::Function
    {
        QtLua::Value::List meta_call(QtLua::State &ls, const QtLua::Value::List &args)
        {
            QDate date = QDate::currentDate();
            return QtLua::Value(ls, date.weekNumber());
        }

    } week_number;



    static class : public QtLua::Function
    {
        QtLua::Value::List meta_call(QtLua::State &ls, const QtLua::Value::List &args)
        {
            QDate date = QDate::currentDate();
            return QtLua::Value(ls, date.month());
        }

    } month_number;

    info_box.register_(plugin, "info");
    warning_box.register_(plugin, "warning");
    get_int.register_(plugin, "get_int");
    get_filename.register_(plugin, "get_filename");
    write_text_file.register_(plugin, "write_text_file");
    week_number.register_(plugin, "week_number");
    month_number.register_(plugin, "month_number");
}

struct LoadUnload
{
    LoadUnload()
    {
        qDebug("Qt tools::plugin loaded");
    }

    ~LoadUnload()
    {
        qDebug("Qt tools::plugin unloaded");
    }
} load_unload;

